<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 18. 数组转对象
    // ['a','a','a','b','b','c','c','c','c','d','d','d','d'] ---> { a: 3, b: 2, c: 4, d: 4 }

    // 即记录一个字符在数组中出现多少次即可
    function exchangeToObject (arr) {
      var obj = {}
      for (var i = 0; i < arr.length; i++) {
        // 有则加1
        if (obj[arr[i]]) {
          obj[arr[i]]++
        } else {
          // 新的则设为1
          obj[arr[i]] = 1
        }
      }
      return obj
    }
    console.log('期望输出{ a: 3, b: 2, c: 4, d: 4 }')
    console.log(exchangeToObject(['a','a','a','b','b','c','c','c','c','d','d','d','d']))
  </script>
</body>
</html>